package com.icid.store.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.icid.store.business.delegate.ProductDelegate;
	import com.icid.store.event.GetProductListEvent;
	import com.icid.store.model.ProductModelLocator;
	import com.icid.store.vo.ProductFilterVO;
	import com.icid.store.vo.ProductVO;
	
	import mx.rpc.IResponder;

	public class GetProductListCommand extends CommandBase implements ICommand, IResponder
	{
		private var delegate:ProductDelegate;
		private var getProductListEvent:GetProductListEvent;
		private var productModel:ProductModelLocator = ProductModelLocator.getInstance();
		public function GetProductListCommand()
		{
			super();
			delegate = new ProductDelegate(this);
		}
		
		override public function execute(event:CairngormEvent):void
		{
			getProductListEvent = GetProductListEvent(event);
			delegate.read(getProductListEvent.productFilterVO);
		}
		
		override public function result(data:Object):void
		{
			productModel.productList.removeAll();
			productModel.minMax.removeAll();
//			trace(data.result);
			var productFilterVO:ProductFilterVO = new ProductFilterVO();
			if(data.result!=null)
			{
				for(var i:int=0;i<data.result.length;i++)
				{
					if(i==0)
					{
						productFilterVO.min = data.result[0].price;
					}
					if(i==data.result.length-1)
					{
						productFilterVO.max = data.result[data.result.length-1].price;
					}
					
					var productVO:ProductVO = new ProductVO();
					productVO.description = data.result[i].description;
					productVO.id = data.result[i].id;
					productVO.image = data.result[i].image;
					productVO.name = data.result[i].name;
					productVO.pdf = data.result[i].pdf;
					productVO.price = data.result[i].price;
					productVO.swf = data.result[i].swf;
					productVO.testcode = data.result[i].testcode;
					productVO.time = data.result[i].time;
					productVO.category = data.result[i].category;
					productVO.mprice = data.result[i].mprice;
					
					productModel.productList.addItem(productVO);
				}
			}
			productModel.minMax.addItem(productFilterVO);
			productModel.productList.refresh();
			productModel.minMax.refresh();
		}
		
	}
}